//
//  PHProgressView.swift
//  PHWeibo
//
//  Created by 潘荟 on 16/7/20.
//  Copyright © 2016年 BAT. All rights reserved.
//

import UIKit

class PHProgressView: UIProgressView {
    
    var timer:NSTimer?
    var isAnim:Bool = true
    

    override init(frame: CGRect) {
        
        
        super.init(frame: CGRect(x: 0, y: 64, width: SCREENW, height: 2))
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI() {
        self.tintColor = UIColor.greenColor()
    }
    
    func startAnim() {
        self.progress = 0
        self.hidden = false
        
        timer = NSTimer.scheduledTimerWithTimeInterval(1/60, target: self, selector: "progressChange", userInfo: nil, repeats: true)
    
    }
    func endAnim() {
        self.hidden = true
    }
    
    func removeView() {
        removeFromSuperview()
    }
    
    func progressChange() {
        
        if isAnim {
            self.progress += 0.05
            if self.progress >= 0.95 {
                self.progress = 0.95
            }
        }else {
            if self.progress == 1 {
                self.hidden = true
                timer?.invalidate()
            }else {
                self.progress += 0.05
            }
        }
        
    }
    
    deinit{
        timer?.invalidate()
    }
    

}
